package cn.murky.admin.system.biz.mq;

import cn.murky.admin.system.biz.service.ISysI18nService;
import cn.murky.redismq.RedisMqTemplate;
import lombok.extern.slf4j.Slf4j;
import org.noear.redisx.RedisClient;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

@Component
@Slf4j
public class I18nRedisMqTemplate implements RedisMqTemplate {
    @Inject
    private RedisClient redisClient;
    @Inject
    private ISysI18nService iSysI18nService;
    public void publish() {
        log.info("[I18nRedisMqTemplate] -> publish 发布国际化语言包清空缓存主题 topic:{}", getTopic());
        redisClient.getBus().publish(getTopic(),null);
    }

    @Override
    public boolean subscribe() {
        log.info("[I18nRedisMqTemplate] -> 订阅国际化语言包清空缓存主题 -> topic:{}", getTopic());
        redisClient.getBus().subscribe((topic, msg) -> {
            iSysI18nService.refreshLocal();
            // 分布式锁,在集群环境下防止重复刷新缓存
            if (redisClient.getLock(getTopic() + ":LOCK").tryLock(5)) {
                log.info("[I18nRedisMqTemplate] -> 消费国际化语言包清空缓存主题 -> topic:{}", topic);
                iSysI18nService.refresh();
            }
        }, getTopic());
        return true;
    }

    @Override
    public String getTopic() {
        return "topic:i18n:clean";
    }
}
